package cn.maidouya.pet.service.impl;

import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.pet.domain.Pet;
import cn.maidouya.pet.mapper.PetDetailMapper;
import cn.maidouya.pet.mapper.PetMapper;
import cn.maidouya.pet.service.IPetService;
import cn.maidouya.pet.domain.Pet;
import cn.maidouya.pet.domain.PetDetail;
import cn.maidouya.user.domain.Logininfo;
import cn.maidouya.user.domain.User;
import cn.maidouya.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public void delete(Long petId) {
        // 1. 先删除关联表(详情页)
        petDetailMapper.deleteByPetId(petId);
        // 2. 先删除主表(服务)
        petMapper.delete(petId);
    }

    /**
     *    添加服务和详情页
     * @param pet
     */
    @Override
    @Transactional
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        if (detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        if (detail.getId() != null){
            detail.setPet_id(pet.getId());
            // 这个时详情页，因为添加服务时候没有添加详情页，这时候详情页可存在空
            if (detail.getId() != null){
                petDetailMapper.update(detail);
            }else {
                petDetailMapper.save(detail);
            }

        }
    }

    @Override
    public PetDetail loadDetailByPetId(Long petId) {
        System.err.println("查询详情页" +petId);
        return petDetailMapper.loadDetailByPetId(petId);
    }

    /**
     *   批量上架和批量下架
     * @param ids
     * @param flag  1上架 0下架
     */
    @Override
    public void onOROffSale(List<Long> ids, int flag) {
        Map<String, Object> map = new HashMap<>();
        if (flag == 1){ // 批量上架
            map.put("onsaletime", new Date());
            map.put("ids",ids);
            petMapper.onSale(map);
        }else {// 批量下架
            map.put("offsaletime", new Date());
            map.put("ids",ids);
            petMapper.offSale(map);
        }
    }
    // 批量删除
    @Override
    public void batchDelete(List<Long> ids) {
        // 前端做了选择较空
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        // 1. 先删除关联表(详情页)
        petDetailMapper.batchDeleteByPetids(map);
        // 2. 先删除主表(服务)
        petMapper.batchDeleteIds(map);

    }
    // 右侧经典搭配查询
    @Override
    public List<Pet> loadMatch() {
        List<Pet> list = petMapper.loadMatch();
        return list;
    }

    @Override
    public Pet loadByPetDetail(Long id) {
        // 宠物和宠物详情页展示
        return petMapper.loadByPetDetail(id);
    }

    // 领养宠物
    @Override
    @Transactional
    public void adopt(Long id, Long logininfoId) {
            // 第一步：修改宠物状态
            Pet pet = petMapper.loadById(id);
            if (pet != null){
                pet.setState(0);
                pet.setOffsaletime(new Date());
                // 第二步：宠物绑定用户
                User user = userMapper.loadByLogininfoId(logininfoId);
                pet.setUser_id(user.getId());
                petMapper.update(pet);
            }

            // 第三步：生成订单支付

    }


}
